package com.merlin.meta.domain.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.merlin.framework.core.bean.PageBean;
import com.merlin.framework.core.exception.ErrorHandler;
import com.merlin.framework.core.util.VoPoConverter;
import com.merlin.meta.domain.dao.MetDomainMapper;
import com.merlin.meta.domain.model.MetDomain;
import com.merlin.meta.domain.service.MetaDomainService;
import com.merlin.meta.domain.vo.MetaDomainAddVo;
import com.merlin.meta.domain.vo.MetaDomainUpdateVo;
import com.merlin.meta.domain.vo.MetaDomainVo;

@Service("metaDomainService")
public class MetaDomainServiceImpl implements MetaDomainService {
	@Autowired
	private MetDomainMapper metDomainMapper;

   /**
            * 新增领域资源服务
    */
	@Override
	public int insertSelective(MetaDomainAddVo metaDomainAddVo) {

		
		  if(metaDomainAddVo.getName()!=null){ ////验证名称不能重复 MetDomainAddVo
		    metaDomainAddVo=VoPoConverter.copyProperties(metaDomainAddVo,MetaDomainAddVo.class); 
		    int flag1=isAddDup(metaDomainAddVo); 
		    if(flag1==1){
		      return 0;
		   } 
		  }
		 	
		MetDomain metDomain = VoPoConverter.copyProperties(metaDomainAddVo, MetDomain.class);
		return metDomainMapper.insertSelective(metDomain);
	}

	/**
	 * 单记录查询领域资源服务
	 */
	@Override
	public MetaDomainVo selectByPrimaryKey(String code) {
		MetDomain metDomain=metDomainMapper.selectByPrimaryKey(code);
		MetaDomainVo metaDomainVo = VoPoConverter.copyProperties(metDomain, MetaDomainVo.class);
		return metaDomainVo;
	}
    /**
              * 查询领域对象列表信息
     */
	@Override
	public List<MetaDomainVo> search(MetaDomainVo metaDomainVo,PageBean pageBean) {
		MetDomain metDomain = VoPoConverter.copyProperties(metaDomainVo, MetDomain.class);
		List<MetDomain> metDomainList=metDomainMapper.search(metDomain, pageBean);
		List<MetaDomainVo> metDomainVoList=VoPoConverter.copyList(metDomainList, MetaDomainVo.class);
		return metDomainVoList;
	}
    /**
               * 查询领域对象列表条数
     */
	@Override
	public int searchCount(MetaDomainVo metaDomainVo) {
		MetDomain metDomain = VoPoConverter.copyProperties(metaDomainVo, MetDomain.class);
		int  count=metDomainMapper.searchCount(metDomain);
		return count;
	}
    /**
            * 删除领域资源服务
     */
	@Override
	public int deleteDomain(String code) {
		int count=metDomainMapper.deleteByPrimaryKey(code);
		return count;
	}
		/**
		 * 更新领域资源服务
		 */
	@Override
	public int alterDomain(MetaDomainUpdateVo metaDomainUpdateVo) {
		MetaDomainUpdateVo metDomainUpdateVo1=VoPoConverter.copyProperties(metaDomainUpdateVo, MetaDomainUpdateVo.class);
		int flag1=isUpdateDup(metDomainUpdateVo1);
		if(flag1==1){
			return 0;
		}  
		MetDomain metDomain = VoPoConverter.copyProperties(metaDomainUpdateVo, MetDomain.class);
		int flag=metDomainMapper.alterDomain(metDomain);
		return flag;
	}
	 /**
	  * 
	 * @Title：isAddDup 
	 * @Description：判断新增时是否重复
	 * @param ：@param metDomainAddVo
	 * @param ：@return 
	 * @return ：int 
	 * @throws
	  */
	private int isAddDup(MetaDomainAddVo metaDomainAddVo){
		MetDomain metDomain = VoPoConverter.copyProperties(metaDomainAddVo, MetDomain.class);
		int count=metDomainMapper.isAddDup(metDomain);
		return count;
	}
	/**
	 * 
	* @Title：isUpdateDup 
	* @Description：更新时判断是否重复
	* @param ：@param metDomainUpdateVo
	* @param ：@return 
	* @return ：int 
	* @throws
	 */
	private int isUpdateDup(MetaDomainUpdateVo metaDomainUpdateVo){
		MetDomain metDomain = VoPoConverter.copyProperties(metaDomainUpdateVo, MetDomain.class);
		int count=metDomainMapper.isUpdateDup(metDomain);
		return count;
	}
}
